using System;
using System.Web.UI;
using SubversionReports.Entities;
using SubversionReports.Utilities;

namespace SubversionReports.Web.Controls
{
    public partial class FolderView : UserControl
    {
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if(Visible)
            {
                ChildPathRepeater.DataSource = SvnWebDav.GetPathChildren(SvnWebDav.CurrentRepositoryRootUrl, SvnWebDav.CurrentRepositoryUsername, SvnWebDav.CurrentRepositoryPassword, Request["Path"], false);
                ChildPathRepeater.DataBind();

                HistoryView1.FileLog = SvnWebDav.GetFileLog(SvnWebDav.CurrentRepositoryRootUrl, SvnWebDav.CurrentRepositoryUsername, SvnWebDav.CurrentRepositoryPassword, Request["Path"]);
            }
        }

        /// <summary>
        /// Gets the icon URL.
        /// </summary>
        /// <param name="isFolder">if set to <c>true</c> [is folder].</param>
        /// <returns></returns>
        protected static string GetIconUrl(bool isFolder)
        {
            return (isFolder ? "Images/folder.png" : "Images/file.png");
        }

        /// <summary>
        /// Determines whether the specified path is folder.
        /// </summary>
        /// <param name="path">The path.</param>
        /// <returns>
        /// 	<c>true</c> if the specified path is folder; otherwise, <c>false</c>.
        /// </returns>
        protected static bool IsFolder(string path)
        {
            return SvnWebDav.IsFolder(path);
        }

        /// <summary>
        /// Gets the navigate URL.
        /// </summary>
        /// <param name="obj">The obj.</param>
        /// <returns></returns>
        protected string GetNavigateUrl(object obj)
        {
            PathSummary pathSummary = obj as PathSummary;
            if(pathSummary != null)
                return (!pathSummary.IsFolder)
                ? string.Format("{0}?RevisionId={1}&Path={2}", Request.FilePath, SvnWebDav.GetLastPathRevisionId(SvnWebDav.CurrentRepositoryRootUrl, SvnWebDav.CurrentRepositoryUsername, SvnWebDav.CurrentRepositoryPassword, pathSummary.FileName), pathSummary.FileName)
                : string.Format("{0}?Path={1}", Request.FilePath, pathSummary.FileName);
            return string.Empty;
        }

        /// <summary>
        /// Gets the trimmed path.
        /// </summary>
        /// <param name="fullPath">The full path.</param>
        /// <returns></returns>
        protected static string GetTrimmedPath(string fullPath)
        {
            return fullPath.Substring(fullPath.LastIndexOf('/') + 1).TrimStart('/');
        }
    }
}